/**
 * Created by zhang on 2016/1/20.
 */
ige.service('TaxSettingService', ['$mdDialog', '$http', '$cookieStore', function ($mdDialog, $http, $cookieStore) {
    var showErrorDlg = function (title, content) {
        $mdDialog.show(
            $mdDialog.alert()
                .clickOutsideToClose(true)
                .title(title)
                .textContent(content)
                .ok('确定')
                .openFrom('#login')
                .closeTo('#login')
        );
    };
    var headers = {
        'Content-Type': 'application/json',
        Accept: 'application/json',
        Authorization: $cookieStore.get('access_token')
    };

    var getApi = ige.systemSetting.apiDomain + 'web/taxscheme/list';
    var deleteApi = ige.systemSetting.apiDomain + 'web/taxscheme/delete';
    var addApi = ige.systemSetting.apiDomain + 'web/taxscheme/add';
    var editApi = ige.systemSetting.apiDomain + 'web/taxscheme/update';

    var result = {
        getTaxSettings: function (pageSkip, pageSize, filter, successCallback) {
            var promise = $http.post(getApi,
                {
                    pageSkip: pageSkip,
                    pageSize: pageSize,
                    filter: filter
                }, {
                    headers: headers
                }).then(function (response) {
                if (response.data.is_ok) {
                    successCallback(response.data.data)
                } else {
                    showErrorDlg('获取税率出错', response.data.message);
                }
            }, function (response) {
                showErrorDlg('获取税率出错', response.statusText);
            });

            return promise;
        },
        getTaxSetting: function (id, successCallback) {
            var promise = $http.post(getApi,
                {
                    filter: {
                        id: id
                    }
                }, {
                    headers: headers
                }).then(function (response) {
                if (response.data.is_ok) {

                    successCallback(response.data.data[0])
                } else {
                    showErrorDlg('获取税率出错', response.data.message);
                }
            }, function (response) {
                showErrorDlg('获取税率出错', response.statusText);
            });
            return promise;
        },
        deleteTaxSetting: function (id, successCallback) {
            var promise = $http.post(deleteApi,
                {id: id}, {
                    headers: headers
                }).then(function (response) {
                if (response.data.is_ok) {
                    successCallback()
                } else {
                    showErrorDlg('删除出错', response.data.message);
                }
            }, function (response) {
                showErrorDlg('删除出错', response.statusText);
            });
            return promise;
        },
        addTaxSetting: function (taxSetting, successCallback) {
            var rules = [];
            for (var inx = 0; inx < taxSetting.Rules.length; inx++) {
                rules.push({
                    index: taxSetting.Rules[inx].Index,
                    lowbound: taxSetting.Rules[inx].Lowbound,
                    upbound: taxSetting.Rules[inx].Upbound,
                    rate: taxSetting.Rules[inx].Rate
                })
            }


            var promise = $http.post(addApi,
                {
                    name: taxSetting.Name,
                    description: taxSetting.Description,
                    rules: rules
                }, {
                    headers: headers
                }).then(function (response) {
                if (response.data.is_ok) {
                    successCallback()
                } else {
                    showErrorDlg('添加税率出错', response.data.message);
                }
            }, function (response) {
                showErrorDlg('添加税率出错', response.statusText);
            });
            return promise;
        },
        editTaxSetting: function (taxSetting, successCallback) {
            var rules = [];
            for (var inx = 0; inx < taxSetting.Rules.length; inx++) {
                rules.push({
                    index: taxSetting.Rules[inx].Index,
                    lowbound: taxSetting.Rules[inx].Lowbound,
                    upbound: taxSetting.Rules[inx].Upbound,
                    rate: taxSetting.Rules[inx].Rate
                })
            }
            var promise = $http.post(editApi,
                {
                    id: taxSetting.Id,
                    name: taxSetting.Name,
                    description: taxSetting.Description,
                    rules: rules
                }, {
                    headers: headers
                }).then(function (response) {
                if (response.data.is_ok) {
                    successCallback()
                } else {
                    showErrorDlg('添加税率出错', response.data.message);
                }
            }, function (response) {
                showErrorDlg('添加税率出错', response.statusText);
            });
            return promise;
        }
    };
    return result;
}]);